/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 张宇田
 * Date: 2025-08-16
 * Time: 17:55
 */
public class Main {
    public static void main(String[] args) {
        Solution solution = new Solution();

        // 示例一
        int[] nums1 = {3, 2, 2, 3};
        int val1 = 3;
        int k1 = solution.removeElement(nums1, val1);
        System.out.println("新数组长度: " + k1);
        System.out.print("数组内容: ");
        for (int i = 0; i < k1; i++) {
            System.out.print(nums1[i] + " ");
        }
        System.out.println();

        // 示例二
        int[] nums2 = {0, 1, 2, 2, 3, 0, 4, 2};
        int val2 = 2;
        int k2 = solution.removeElement(nums2, val2);
        System.out.println("新数组长度: " + k2);
        System.out.print("数组内容: ");
        for (int i = 0; i < k2; i++) {
            System.out.print(nums2[i] + " ");
        }
        System.out.println();
    }
}

class Solution {
    public int removeElement(int[] nums, int val) {
        int k = 0; // 新数组的长度
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != val) {   // 保留元素
                nums[k] = nums[i];  // 覆盖到前面
                k++;
            }
        }
        return k;
    }
}

